import { message } from 'antd'
import axios from 'axios'
import qs from 'qs'
import history from './history'
import { getToken, removeToken } from './storage'
export const baseURL = 'http://geek.itheima.net'
const instance = axios.create({
  baseURL: baseURL,
  timeout: 5000,
})

// 请求拦截器
instance.interceptors.request.use(
  function (config) {
    const token = getToken()
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  // 请求拦截器和响应拦截器的error对应
  function (error) {
    return Promise.reject(error)
  }
)
// 响应拦截器
instance.interceptors.response.use(
  function (response) {
    console.log('1111111')
    return response.data
  },
  function (error) {
    if (error.response && error.response.status === 401) {
      removeToken()
      console.log('first')
      history.push('/login')
      message.warn(error.response.data.message, 1)
      console.log(error.response)
    } else {
      // 断网处理
      message.warn('刷新失败，请检查网络', 1)
    }
    // console.log('sdasj')
    return Promise.reject(error)
  }
)
const ParamsUrl = (url = '', data = {}, method = '') => {
  if (method === 'GET') {
    return new Promise((resolve, reject) => {
      let params = data
      instance
        .get(url, {
          params: params,
          paramsSerializer: function (params) {
            return qs.stringify(params, {})
          },
        })
        .then((res) => {
          if (res) {
            resolve(res)
          } else {
            resolve(res)
          }
        })
        .catch((err) => {
          reject(err)
        })
    })
  }

  if (method === 'POST') {
    return new Promise((resolve, reject) => {
      instance({
        method: 'POST',
        url: url,
        data: JSON.stringify(data),
        headers: {
          'Content-Type': '	application/json',
        },
      })
        .then((res) => {
          if (res) {
            resolve(res)
          } else {
            resolve(res)
          }
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
  if (method === 'DELETE') {
    return new Promise((resolve, reject) => {
      let params = data
      instance({
        method: 'DELETE',
        url: url,
        params: params,
        headers: {
          'Content-Type': '	application/json',
        },
      })
        .then((res) => {
          console.log(res)
          if (res) {
            resolve(res)
          } else {
            resolve(res)
          }
        })
        .catch((err) => {
          reject(err)
        })
    })
  }

  if (method === 'PUT') {
    return new Promise((resolve, reject) => {
      instance({
        method: 'PUT',
        url: url,
        params: qs.stringify({
          data: JSON.stringify(data),
        }),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        },
      })
        .then((res) => {
          if (res) {
            resolve(res.data)
          } else {
            resolve(res)
          }
        })
        .catch((err) => {
          reject(err)
        })
    })
  }

  if (method === 'DELETE') {
    return new Promise((resolve, reject) => {
      let params = {
        data: JSON.stringify(data),
      }
      instance
        .delete(url, {
          params: params,
          paramsSerializer: function (params) {
            return qs.stringify(params, {
              arrayFormat: 'brackets',
            })
          },
        })
        .then((res) => {
          if (res) {
            resolve(res.data)
          } else {
            resolve(res)
          }
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
}
export default ParamsUrl
